package com.dman.multi.ui;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Calendar;

import org.apache.pivot.beans.BXML;
import org.apache.pivot.beans.Bindable;
import org.apache.pivot.collections.Map;
import org.apache.pivot.util.CalendarDate;
import org.apache.pivot.util.Resources;
import org.apache.pivot.util.Vote;
import org.apache.pivot.wtk.BoxPane;
import org.apache.pivot.wtk.Button;
import org.apache.pivot.wtk.Button.State;
import org.apache.pivot.wtk.ButtonPressListener;
import org.apache.pivot.wtk.CalendarButton;
import org.apache.pivot.wtk.Checkbox;
import org.apache.pivot.wtk.Dialog;
import org.apache.pivot.wtk.FileBrowserSheet;
import org.apache.pivot.wtk.PushButton;
import org.apache.pivot.wtk.Sheet;
import org.apache.pivot.wtk.SheetCloseListener;
import org.apache.pivot.wtk.Spinner;
import org.apache.pivot.wtk.TextInput;
import org.apache.pivot.wtk.TextInputContentListener;

import com.dman.multi.core.FileStream;

public class AddDownload extends Dialog implements Bindable {
	
	@BXML private TextInput url;
	@BXML private TextInput folder;
	@BXML private PushButton browse;
	@BXML private Checkbox autoGenerate;
	@BXML private TextInput fileName;
	@BXML private Checkbox scheduleStart;
	@BXML private BoxPane startTime;
	@BXML private CalendarButton startDate;
	@BXML private Spinner startHr;
	@BXML private Spinner startMin;
	@BXML private Checkbox scheduleStop;
	@BXML private BoxPane stopTime;
	@BXML private CalendarButton stopDate;
	@BXML private Spinner stopHr;
	@BXML private Spinner stopMin;
	@BXML private PushButton okButton;
	@BXML private PushButton cancelButton;

	@Override
	public void initialize(Map<String, Object> namespace, URL location,
			Resources resources) {
		// setup listeners for folder input
		folder.getTextInputContentListeners().add(new TextInputContentListener.Adapter(){
			@Override
			public Vote previewInsertText(TextInput textInput,
					CharSequence text, int index) {
				return Vote.DENY;
			}			
			@Override
			public Vote previewRemoveText(TextInput textInput, int index,
					int count) {
				return Vote.DENY;
			}
		});
		
		// setup listeners for browse button
		String fldr = System.getProperty("user.home", "~") + File.separator + "Downloads";
		this.folder.setText(fldr);
		browse.getButtonPressListeners().add(new ButtonPressListener() {			
			@Override
			public void buttonPressed(Button button) {
				final FileBrowserSheet fbs = new FileBrowserSheet(FileBrowserSheet.Mode.SAVE_TO);
				fbs.open(AddDownload.this, new SheetCloseListener() {					
					@Override
					public void sheetClosed(Sheet sheet) {
						if(sheet.getResult()){
							folder.setText(fbs.getSelectedFile().getAbsolutePath());
						}
					}
				});
			}
		});
		
		// setup auto-generate file names
		autoGenerate.getButtonPressListeners().add(new ButtonPressListener() {			
			@Override
			public void buttonPressed(Button button) {
				boolean enabled = true;
				fileName.clear();
				if(autoGenerate.getState() == State.SELECTED)
					enabled = false;
				fileName.setEnabled(enabled);
			}
		});
		
		// setup the scheduler
		scheduleStart.getButtonPressListeners().add(new ButtonPressListener() {			
			@Override
			public void buttonPressed(Button button) {
				boolean enabled = false;
				if(scheduleStart.getState() == State.SELECTED)
					enabled = true;
				startTime.setEnabled(enabled);
			}
		});
		scheduleStop.getButtonPressListeners().add(new ButtonPressListener() {			
			@Override
			public void buttonPressed(Button button) {
				boolean enabled = false;
				if(scheduleStop.getState() == State.SELECTED)
					enabled = true;
				stopTime.setEnabled(enabled);
			}
		});
		
		// setup the ok-cancel buttons
		okButton.getButtonPressListeners().add(new ButtonPressListener() {			
			@Override
			public void buttonPressed(Button button) {
				AddDownload.this.close(true);
			}
		});
		cancelButton.getButtonPressListeners().add(new ButtonPressListener() {			
			@Override
			public void buttonPressed(Button button) {
				AddDownload.this.close(false);
			}
		});
	}

	public void init(boolean cmdAdd){
		State startState = State.SELECTED;
		boolean enabled = true;
		if(cmdAdd){
			startState = State.UNSELECTED;
			enabled = false;
		}		
		scheduleStart.setState(startState);
		startTime.setEnabled(enabled);
		scheduleStop.setState(State.UNSELECTED);
		stopTime.setEnabled(false);
	}
	
	public String getUrl(){
		return url.getText();
	}
	
	public String getFolderName(){
		return folder.getText();
	}
	
	public String getFileName() throws MalformedURLException{
		String fname = FileStream.getFileName(new URL(url.getText()));;
		if(autoGenerate.getState() == State.UNSELECTED)
			fname = fileName.getText();
		return fname;
	}
	
	public long getScheduledStart(){
		long timeInMillis = Calendar.getInstance().getTimeInMillis();
		if(scheduleStart.getState() == State.SELECTED){
			CalendarDate cd = startDate.getSelectedDate();
			Calendar cal = cd.toCalendar();
			timeInMillis = cal.getTimeInMillis() + startHr.getSelectedIndex() * 3600000 + startMin.getSelectedIndex() * 60000;
		}
		return timeInMillis;
	}
	
	public long getScheduledStop(){
		long timeInMillis = 0;
		if(scheduleStop.getState() == State.SELECTED){
			CalendarDate cd = stopDate.getSelectedDate();
			Calendar cal = cd.toCalendar();
			timeInMillis = cal.getTimeInMillis() + stopHr.getSelectedIndex() * 3600000 + stopMin.getSelectedIndex() * 60000;
		}
		return timeInMillis;
	}
}
